﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using System.Web.Script.Serialization;
using System.Web.Script.Services;
using Intelsi.Entity;
using Intelsi.BusinessLogic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace Intelsi.WB.Service
{
    /// <summary>
    /// Descripción breve de UbigeoService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // Para permitir que se llame a este servicio Web desde un script, usando ASP.NET AJAX, quite la marca de comentario de la línea siguiente. 
    // [System.Web.Script.Services.ScriptService]
    public class UbigeoService : System.Web.Services.WebService
    {
        [Dependency]
        public IUbigeoBL ubigeoBL { get; set; }

        [Dependency]
        public IBancoBL bancoBL { get; set; }

        public UbigeoService()
        {
            using (var container = new UnityContainer())
            {
                container.LoadConfiguration();
                container.BuildUp(this.GetType(), this);
            }
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]

        public void GetAllFromUbigeoByCriterio(string term)
        {
    //           var departamento="";

            var query = from p in ubigeoBL.GetAllFromUbigeoByCriterio(term)
                       
                        orderby (p.id) descending
 
                        select new
                        {
                            value = p.id,
                            label = p.distrito,
                            ubigeo = new
                            {
                                departamento = p.departamento,
                                provincia = p.provincia,
                               
                            }

                           
                        };

                                            

            var json = new JavaScriptSerializer();

            Context.Response.ContentType = "application/json; charset=utf-8";
            Context.Response.Write(json.Serialize(query.Skip(0).Take(10).ToList()));
            Context.Response.End();

        }


        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]

        public void GetAllFromBancoByCriterio(string term)
        {

            var query = from b in bancoBL.GetAllFromBancoByCriterio(term) 

                        orderby (b.IdBanco) descending

                        select new
                        {
                            value = b.IdBanco,
                            label = b.Nombre,
                            //ubigeo = new
                            //{
                            //     = p.departamento,
                            //    provincia = p.provincia,

                            //}


                        };



            var json = new JavaScriptSerializer();

            Context.Response.ContentType = "application/json; charset=utf-8";
            Context.Response.Write(json.Serialize(query.Skip(0).Take(10).ToList()));
            Context.Response.End();

        }
    }
}
